using System;
using System.Data;
using System.Configuration;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using Biblioteca.DataBase;
using SEG.Utils;
using System.Data.OracleClient;
using Pms.Utils;

namespace SEG.Objetos
{
    public class Parametro
    {
        private string cod_param;
        private string dsc_param;
        private string valor;

        public Parametro(string cod_param, string valor)
        {
            this.cod_param = cod_param;
            this.valor = valor;
        }

        public Parametro()
        {
        }

        public Parametro(string cod_param, string dsc_param, string valor)
        {
            this.dsc_param = dsc_param;
            this.cod_param = cod_param;
            this.valor = valor;
        }

        public string Cod_param
        {
            get { return cod_param; }
            set { cod_param = value; }
        }

        public string Dsc_param
        {
            get { return dsc_param; }
            set { dsc_param = value; }
        }

        public string Valor
        {
            get { return valor; }
            set { valor = value; }
        }
        
        //Pesquisa de produto por codigo
        public static Parametro Pesquisa(string cod_param, string login)
        {
            Parametro param;
            string sql = "SELECT * " +
                          " FROM SEG_PARAMETROS " +
                          " WHERE cod_param=:cod_param ";
            Query qry = new Query(BiblioPMS.ObterConexao(BiblioPMS.aplicacaoSEG, login), sql);
            try
            {
                qry.AdicionarParametro(":cod_param", DbType.String, cod_param);
                qry.Executar();
                param = new Parametro(qry.ObterCampo("COD_PARAM"), qry.ObterCampo("DSC_PARAM"), qry.ObterCampo("VALOR"));
            }
            catch
            {
                qry.FecharConexao();
                return new Parametro();
            }
            qry.FecharConexao();
            return param;
        }

        public void Alterar(string loginAut)
        {
            OracleConnection con = BiblioPMS.ObterConexao(BiblioPMS.aplicacaoSEG, loginAut);
            try
            {
                OracleCommand command = new OracleCommand("seg_pkg.alterar_parametro", con);
                command.CommandType = CommandType.StoredProcedure;
                command.Parameters.Add(new OracleParameter("p_cod", OracleType.Number));
                command.Parameters.Add(new OracleParameter("p_valor", OracleType.VarChar));
                command.Parameters["p_cod"].Value = this.cod_param;
                command.Parameters["p_valor"].Value = this.valor;
                command.ExecuteNonQuery();
            }
            catch (Exception ex)
            {
                throw new Exception(ex.Message);
            }
            finally
            {
                con.Close();
            }
        }
    }
}
